import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.young.gpg.common.util.SFTPUtil;

import java.io.File;
import java.io.InputStream;

/**
 * @description：
 * @author: yangyahui01
 * @date: 12/4/24 2:04 PM
 */
public class SSHClient {
    public static void main(String[] args) {
        String host = "hadoop102"; // 服务器IP地址
        int port = 22; // SSH端口
        String username = "atguigu"; // SSH用户名
        String privateKeyResourcePath = "/ssh/ssh_p_privateKey.txt"; // 相对于resources目录的私钥文件路径


        JSch jsch = new JSch();



        try {

            File privateKeyFile = new File(SSHClient.class.getResource(privateKeyResourcePath).toURI());


            SFTPUtil sftpUtil = new SFTPUtil(username, null, port, host, privateKeyFile.getAbsolutePath(), null);

            ChannelSftp sftp = sftpUtil.privateKeyChannel();

            InputStream stream = SSHClient.class.getResourceAsStream(privateKeyResourcePath);

            boolean flag = sftpUtil.upload(stream, "/home/atguigu/test.txt", sftp);

            System.out.println(flag);



           /* Properties config=new Properties();
            config.put("StrictHostKeyChecking", "no"); // 跳过主机密钥检查
            config.put("kex","diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,curve25519-sha256@libssh.org");


            session.setConfig(config);*/


           /* // 设置主机密钥算法
            session.setConfig("HostKeyAlgorithms", "+ssh-rsa");

            // 设置公钥接受的类型
            session.setConfig("PubkeyAcceptedKeyTypes", "+ssh-rsa");

            // 设置其他配置
            session.setConfig("ServerAliveInterval", "30");
            session.setConfig("ControlMaster", "auto");
            session.setConfig("ControlPath", "/tmp/master-%r@%h:%p");
            session.setConfig("ControlPersist", "yes");
            session.setConfig("StrictHostKeyChecking", "no");

            session.setTimeout(200000);

            System.setProperty("jsch.debug", "ALL");

            session.connect();
            System.out.println("Connected to server successfully");


            Channel channel = session.openChannel("sftp");

            channel.connect();

            ChannelSftp sftp = (ChannelSftp) channel;

            Vector ls = sftp.ls("/opt");

            System.out.println(ls);*/

           /* // 在这里执行需要的命令或操作
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand("your-command-here");
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            InputStream in = channel.getInputStream();
            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    System.out.print(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    System.out.println("Exit status: " + channel.getExitStatus());
                    break;
                }
                try { Thread.sleep(1000); } catch (Exception e) {}
            }

            channel.disconnect();
            session.disconnect();*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
